﻿<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="2.0"
                xmlns:cad="http://mitre.org/emevent/0.8/cad2cad/"
                xmlns:emevent="http://mitre.org/emevent/0.8/"
                xmlns:mof="http://example.com/milops/1.1/"
                xmlns:msxsl="urn:schemas-microsoft-com:xslt"
                xmlns:nc="http://release.niem.gov/niem/niem-core/3.0/"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:csharp="http://csharp.org"
                xmlns:str="http://exslt.org/strings"
                extension-element-prefixes="str"
                xmlns:gml="http://www.opengis.net/gml/3.2">

  <xsl:output method="html" indent="yes" exclude-result-prefixes="msxsl csharp"/>
  
  <msxsl:script language="CSharp" implements-prefix="csharp">
    <![CDATA[

	public string getFormattedGML(string input)
	{
    string[] pieces = input.Split(new string[]{" "}, StringSplitOptions.None);
    return "LAT: " + pieces[0] + " LON: " + pieces[1];
	}
  
  	public string getFormattedGMLforGoogleMapURL(string input)
	{
    string[] pieces = input.Split(new string[]{" "}, StringSplitOptions.None);
    return pieces[0] + "," + pieces[1];
	}
  
	]]>
  </msxsl:script>
  <xsl:template match="cad:Event">
    <xsl:variable name="eventStyle">
      <xsl:if test="cad:IncidentDetail">incident</xsl:if>
      <xsl:if test="cad:ResourceDetail">resource</xsl:if>
    </xsl:variable>
    <html xmlns="http://www.w3.org/1999/xhtml">
      <head>
        <title>
          <xsl:if test="cad:IncidentDetail">
            Incident Details for <xsl:value-of select="mof:EventID/nc:IdentificationID"/>
          </xsl:if>
          <xsl:if test="cad:ResourceDetail">
            Incident Details for <xsl:value-of select="mof:EventID/nc:IdentificationID"/>
          </xsl:if>
        </title>
        <style type="text/css">
          body {
          font-family: Arial;
          font-size:100%;
          }

          #container {
          width: 800px;
          margin-left: auto;
          margin-right: auto;

          }

          #alert {
          width: 8em;
          height: 8em;
          margin-top: 1.2em;
          padding-right: 0.8em;

          }

          #alertImage {
          padding-bottom: 0.5em;
          margin-left: 3em;
          }

          .eventHeader{
          margin-left:50px;
          }

          #resource {
          background-image: -ms-linear-gradient(top, #FFFFFF 0%, #056AEF 100%);
          background-image: -moz-linear-gradient(top, #FFFFFF 0%, #056AEF 100%);
          background-image: -o-linear-gradient(top, #FFFFFF 0%, #056AEF 100%);
          background-image: -webkit-gradient(linear, left top, left bottom, color-stop(0, #FFFFFF), color-stop(1, #056AEF));
          background-image: -webkit-linear-gradient(top, #FFFFFF 0%, #056AEF 100%);
          background-image: linear-gradient(to bottom, #FFFFFF 0%, #056AEF 100%);
          filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#fefcea', endColorstr='#f1da36',GradientType=0 ); /* IE6-9 */
          }

          #incident {
          background-image: -o-linear-gradient(-89deg, #FFFFFF 0%, #FEF367 100%);
          background-image: -moz-linear-gradient(-89deg, #FFFFFF 0%, #FEF367 100%);
          background-image: -ms-linear-gradient(-89deg, #FFFFFF 0%, #FEF367 100%);
          background-image: linear-gradient(-179deg, #FFFFFF 0%, #FEF367 100%);
          filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#fefcea', endColorstr='#f1da36',GradientType=0 ); /* IE6-9 */
          }

          .frame {
          border: 0.3em solid #000000;
          border-radius: 1em;
          padding:25px;
          margin-top: -70px;
          margin-left: 110px;
          }

          #data {

          padding-top: 1.2em;


          }

          h1,h3,h4 {
          margin-top: 0em;
          margin-bottom: 0em;
          }

          h1 {
          margin-top: 0.125em;
          font-size: 3em;
          font-weight: bolder;
          }
          h3 {
          font-size: 1.3em;
          }

          h4 {
          font-weight: normal;
          }
          tr:nth-child(odd) {
          background-color: white;
          }

          table {
          border-collapse: separate;
          border-spacing: 0;
          width: 100%;
          margin-bottom: 1.4em;
          }
          tr:nth-child(even) {
          background-color: lightgray;
          }

          tr{
          height: 1.5em;
          }
          td {
          width: 50%;
          word-break:break-all;
          }
          table tr td {
          border-right: 0.15em solid black;
          padding: 0.3125em;
          }
          table tr th:first-child,
          table tr td:first-child {
          border-left: 0.15em solid black;
          }

          table tr td:first-child {
          border-left: 0.15em solid black;
          }

          table tr:first-child {
          border-top: 0.15em solid black;
          }
          table tr td:last-child {
          border-right: 0.15em solid black;
          }

          table tr:first-child td:first-child {
          border-top-left-radius: 0.375em;
          border-top: 0.15em solid black;
          }
          /* top-right border-radius */
          table tr:first-child td:last-child {
          border-top-right-radius: 0.375em;
          border-top: 0.15em solid black;
          }

          /* bottom-left border-radius */
          table tr:last-child td:first-child {
          border-bottom-left-radius: 0.375em;
          border-bottom: 0.15em solid black;
          }

          /* bottom-right border-radius */
          table tr:last-child td:last-child {
          border-bottom-right-radius: 0.375em;
          border-bottom: 0.15em solid black;

          }
          ul,li{
          list-style-type: none;
          list-style-position:inside;
          margin:0;
          padding:0;
          }

          .textBlock{
          border: 0.15em solid black;
          border-radius: 0.375em;
          height: 9.98em;
          background-color: white;
          }

          #addressInfo {
          border: 0.15em solid black;
          height: 3em;
          background-color: white;
          margin-top: 0.3125em;
          margin-bottom: 1.4em;
          }

          .innerText {
          margin: 0.2em;
          padding: 0.2em;
          font-size: 1em;

          }
          #mapContainer {
          height: 10.02em;
          position: relative;

          }

          #map {
          border: 0.15em solid black;
          border-radius: 0.375em;
          position:absolute;
          left:0;
          top:0;
          margin-bottom: 0em;
          height: 10.02em;
          }

          #mapText
          {
          z-index:100;
          position:absolute;
          color:black;
          font-size:1.3em;
          font-weight:bold;
          left:0;
          top:0;
          background: white;
          margin-top: 0.2em;
          margin-left: 0.2em;
          }

          #data h3{
          font-size:1.15em;
          margin-bottom: 5px;
          }
        </style>
      </head>
      <body>
        <div id="container">
          <div id="alertImage">
            <img id="alert" src="triangle.png"/>
          </div>
          <div id="{$eventStyle}" class="frame">
            <div class="eventHeader">
              <h1>
                <xsl:variable name="lower" select="'abcdefghijklmnopqrstuvwxyz'" />
                <xsl:variable name="uppercase" select="'ABCDEFGHIJKLMNOPQRSTUVWXYZ'" />
                <xsl:value-of select="translate($eventStyle,$lower,$uppercase)"/>
              </h1>
              <h4>
                Message <xsl:value-of select="mof:EventID/nc:IdentificationID"/> valid until:
              </h4>
              <h3>
                <xsl:value-of select="mof:EventValidityDateTimeRange/nc:EndDate/nc:DateTime"/>
              </h3>
            </div>
            <div id="data">
              <h3>Core Data:</h3>
              <table>
                <tr>
                  <td>
                    <b>Event Type Code</b>
                  </td>
                  <td>
                    <xsl:value-of select="emevent:EventTypeDescriptor/emevent:EventTypeCode"/>
                  </td>
                </tr>
                <tr>
                  <td>
                    <b>Start Date</b>
                  </td>
                  <td>
                    <xsl:value-of select="mof:EventValidityDateTimeRange/nc:StartDate/nc:DateTime"/>
                  </td>
                </tr>
                <tr>
                  <td>
                    <b>End Date</b>
                  </td>
                  <td>
                    <xsl:value-of select="mof:EventValidityDateTimeRange/nc:EndDate/nc:DateTime"/>
                  </td>
                </tr>
                <tr>
                  <td>
                    <b>USNG Coordinate</b>
                  </td>
                  <td>
                    <xsl:apply-templates select="*[local-name()='USNGCoordinate']"/>
                  </td>
                </tr>
              </table>
              <h3>Address Information:</h3>
              <table>
                <xsl:apply-templates select="*/*/nc:Address"/>
                <xsl:apply-templates select="*/*/nc:AddressCrossStreet"/>
              </table>
              <h3>Event Supporting Information:</h3>
              <table>
                <xsl:apply-templates select="cad:IncidentDetail"/>
                <xsl:apply-templates select="cad:ResourceDetail"/>
              </table>
              <h3>Community Extension Data:</h3>
              <table>
                <xsl:apply-templates select="emevent:EventCommunityExtensions"/>
              </table>
              <div id="mapContainer">
                <xsl:variable name="latLonForMap">
                  <xsl:value-of select="csharp:getFormattedGMLforGoogleMapURL(mof:EventLocation/mof:LocationCylinder/mof:LocationPoint/gml:Point/gml:pos)"/>
                </xsl:variable>
                <p id="mapText">
                  <xsl:value-of select="csharp:getFormattedGML(mof:EventLocation/mof:LocationCylinder/mof:LocationPoint/gml:Point/gml:pos)"/>
                </p>
                <img id="map" src="http://maps.google.com/maps/api/staticmap?zoom=12&amp;size=712x200&amp;markers=color:blue|shadow:false|icon:/Resources/Vehicle_Ambulance.png|{$latLonForMap}&amp;sensor=false"/>
              </div>
            </div>
          </div>
        </div>
      </body>
    </html>
  </xsl:template>
  <xsl:template match="nc:Address">
    <xsl:apply-templates select="nc:LocationStreet"/>
    <tr>
      <td>
        <b>LocationCityName</b>
      </td>
      <td>
        <xsl:value-of select="//nc:Address/nc:LocationCityName"/>
      </td>
    </tr>
  </xsl:template>
  <xsl:template match="nc:AddressCrossStreet">
    <xsl:apply-templates select="nc:LocationStreet"/>
  </xsl:template>
  <xsl:template match="nc:LocationStreet">
    <xsl:for-each select="*[starts-with(local-name(),'Street')]">
      <tr>
        <td>
          <b>
            <xsl:value-of select="local-name()"/>
          </b>
        </td>
        <td>
          <xsl:value-of select="text()"/>
        </td>
      </tr>
    </xsl:for-each>
  </xsl:template>
  <xsl:template match="*[local-name() = 'USNGCoordinate']">
    <xsl:value-of select="*[local-name() = 'USNGGridZoneID']"/>
    <xsl:value-of select="*[local-name() = 'USNGGridZoneSquareID']"/>
    <xsl:value-of select="*[local-name() = 'USNGEastingValue']"/>
    <xsl:value-of select="*[local-name() = 'USNGNorthingValue']"/>
  </xsl:template>
  <xsl:template match="cad:IncidentDetail">
    <tr>
      <td>
        <b>Owning Org</b>
      </td>
      <td>
        <xsl:apply-templates select="cad:IncidentOwningOrganization/nc:OrganizationIdentification/nc:IdentificationID"/>
      </td>
    </tr>
    <tr>
      <td>
        <b>Incident Id</b>
      </td>
      <td>
        <xsl:apply-templates select="cad:IncidentOwningOrganization/cad:IncidentIdentifier/nc:IdentificationID"/>
      </td>
    </tr>
    <tr>
      <td>
        <b>Incident Status</b>
      </td>
      <td>
        <xsl:apply-templates select="cad:IncidentStatus/cad:IncidentPrimaryStatus"/>
      </td>
    </tr>
  </xsl:template>
  <xsl:template match="cad:ResourceDetail">
    <tr>
      <td>
        <b>Owning Org</b>
      </td>
      <td>
        <xsl:apply-templates select="cad:ResourceOwningOrganization/nc:OrganizationIdentification/nc:IdentificationID"/>
      </td>
    </tr>
    <tr>
      <td>
        <b>Resource Id</b>
      </td>
      <td>
        <xsl:apply-templates select="cad:ResourceOwningOrganization/cad:ResourceIdentifier/nc:IdentificationID"/>
      </td>
    </tr>
    <tr>
      <td>
        <b>Resource Status</b>
      </td>
      <td>
        <xsl:apply-templates select="cad:ResourceStatus/cad:ResourcePrimaryStatus"/>
      </td>
    </tr>
  </xsl:template>
  <xsl:template match="emevent:EventCommunityExtensions">
    <xsl:for-each select="*">
      <tr>
        <td>
          <b>
            <xsl:value-of select="local-name()"/>
          </b>
        </td>
        <td>
          <xsl:value-of select="text()"/>
        </td>
      </tr>
    </xsl:for-each>
  </xsl:template>
</xsl:stylesheet>